package com.attilax.lang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.attilax.core;


/**
 * com.attilax.lang.SerialUtil
 * @author Administrator
 *
 */
public class SerialUtil {
	
	public static void main(String[] args) {
	//	System.out.println(read("D:/AtiBrow/index_movs/cateIndexs/1.txt"));
		Map m=new HashMap();
		m.put("2", "我是谁");
		write(m,"z:\\1.txt");
		Object read = read("z:\\1.txt");
		System.out.println("--output::");
		System.out.println(core.toJsonStrO88(read) );	System.out.println("--f::");
	}

	public static void write(Object obj, String file) {
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(file));

			// 步骤二：通过对象输出流的writeObject()s方法写对象：

			out.writeObject(obj);

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}

	}

	public static Object read(String movListssSSeriialOBjFile) {
		if(!new File(movListssSSeriialOBjFile).exists())
			throw new RuntimeException("file not exist "+movListssSSeriialOBjFile+"");
		ObjectInputStream in = null;
		try {
			in = new ObjectInputStream(new FileInputStream(
					movListssSSeriialOBjFile));

			// 步骤二：通过对象输出流的readObject()方法读取对象：
			//ObjectInputStream.readObject

			Object readObject = in.readObject();
			if(readObject==null)
				throw new RuntimeException("readObject is null  from:"+movListssSSeriialOBjFile);
			return readObject;
		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
